package cn.est.controller;

import cn.est.dto.Result;
import cn.est.exceptions.RedisException;
import cn.est.exceptions.TokenProtectedException;
import cn.est.service.TokenService;
import cn.est.utils.ResultUtils;
import cn.est.vo.TokenVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName TokenController
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2021/4/6 11:25
 * @Version 1.0
 **/
@Api("token")
@RestController
@RequestMapping("/token")
public class TokenController {


    @Autowired
    TokenService tokenService;



    @PostMapping(value = "/refresh",produces = "application/json")
    public Result refreshToken(@RequestHeader("token") String token ){
        TokenVo tokenVo=null;
        try {
            tokenService.refreshToken(token);
        } catch (RedisException e) {
            e.printStackTrace();
            return ResultUtils.returnFail(e.getMessage(),"20006");
        } catch (TokenProtectedException e) {
            e.printStackTrace();
            return ResultUtils.returnFail(e.getMessage(),"20007");
        }
        return  ResultUtils.returnDataSuccess(tokenVo);

    }
}
